﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Giaiphongmatbang.BLL;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang
{
    public partial class quanlythongticanhan : System.Web.UI.Page
    {
        User user;
        QLGPMBEntities db = new QLGPMBEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)Session["User"];
            if (!IsPostBack)
            {
                if (user != null)
                {
                    setControl(false);
                    BindData(user);
                }
                btnApply.Enabled = false;
            }
        }

        protected void setControl(bool e)
        {
            txtAddress.Enabled = e;
            txtDisplayName.Enabled = e;
            txtEmail.Enabled = e;
            txtFullName.Enabled = e;
            txtPhone.Enabled = e;
            ddlGender.Enabled = e;
        }

        protected void BindData(User u)
        {
            ltrUserName.Text = u.UserName;
            txtPhone.Text = u.PhoneNumber;
            txtFullName.Text = u.FullName;
            txtEmail.Text = u.Email;
            txtDisplayName.Text = u.DisplayName;
            txtAddress.Text = u.Address;
            ddlGender.SelectedValue = u.Gender.ToString();
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            User newU = db.Users.FirstOrDefault(p => p.UserID == user.UserID);
            newU.Address = txtAddress.Text.Trim();
            newU.DisplayName = txtDisplayName.Text.Trim();
            newU.Email = txtEmail.Text.Trim();
            newU.FullName = txtFullName.Text.Trim();
            newU.PhoneNumber = txtPhone.Text.Trim();
            newU.Gender = bool.Parse(ddlGender.SelectedValue);
            newU.LastModifiedBy = user.UserID;
            newU.LastModifiedDate = DateTime.Now;
            db.SaveChanges();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('Cập nhật thành công')", true);
            Session["User"] = newU;
            BindData(newU);
        }
    }
}